package org.jeecg.modules.openai.controller;


import cn.hutool.json.JSONObject;
import com.theokanning.openai.edit.EditRequest;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.openai.model.*;
import org.jeecg.modules.openai.service.IAiOrderService;
import org.jeecg.modules.openai.service.OpenApiService;
import org.jeecg.modules.openai.service.WenXinService;
import org.jeecg.modules.openai.util.WeChatPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/view/openAi")
@Slf4j
public class KitController {

    @Autowired
    private OpenApiService openApiService;

    @PostMapping("/inputChat")
    public Result input(@RequestBody ChatModel chatModel){


        return Result.OK(openApiService.gptChat(chatModel.getInput()));
    }

    @PostMapping("/write")
    public Result input(@RequestBody WriteModel writeModel){

        String prompt = "我是一个"+writeModel.getRole()+"请根据我给的内容：\n\""+writeModel.getSubject()+"\"帮我生成一个"+(writeModel.getMarkdown()?"markdown格式的":"")+writeModel.getType();
        writeModel.setPrompt(prompt);
        return Result.OK(openApiService.write(writeModel.getPrompt()));
    }
    @PostMapping("/shop")
    public Result shop(@RequestBody ShopModel writeModel){

        String prompt = "用"+writeModel.getType()+"的语气，介绍一个商品"+writeModel.getName()+"请根据我给的商品关键字：\n\""+writeModel.getKeyword()+"\"帮我生成一个商品文案";
        writeModel.setPrompt(prompt);
        return Result.OK(openApiService.write(writeModel.getPrompt()));
    }
    @PostMapping("/redBook")
    public Result input(@RequestBody RedBook writeModel){

        return Result.OK(openApiService.redBook(writeModel));
    }

    @Autowired
    private WenXinService wenXinService;
    @GetMapping("/wenXinGetImg")
    public Result wenXinGetImg(String msg){

        return Result.OK(wenXinService.wenXinGetImg(msg));
    }

    @PostMapping("/wenXinCreateImg")
    public Result wenXinCreateImg(@RequestBody WenXinImageModel imageModel) throws UnsupportedEncodingException {

        return Result.OK(wenXinService.createImg(imageModel));
    }

    @PostMapping("/simpleCreateImg")
    public Result simpleCreateImg(@RequestBody ChatModel chatModel){

        return Result.OK(openApiService.simpleCreateImg(chatModel.getInput()));
    }
    @PostMapping("/naturalLanguageToSQL")
    public Result naturalLanguageToSQL(@RequestBody ChatModel chatModel){
        return Result.OK(openApiService.naturalLanguageToSQL(chatModel.getInput()));
    }
    @PostMapping("/calculateTimeComplexity")
    public Result calculateTimeComplexity(@RequestBody ChatModel chatModel){
        return Result.OK(openApiService.calculateTimeComplexity(chatModel));
    }
    @PostMapping("/bugFixer")
    public Result bugFixer(@RequestBody ChatModel chatModel){
        return Result.OK(openApiService.bugFixer(chatModel));
    }
    @PostMapping("/explainCode")
    public Result explainCode(@RequestBody ChatModel chatModel){
        return Result.OK(openApiService.explainCode(chatModel));
    }
    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @PostMapping("/editImg")
    public Result editImg(@RequestBody EditImg editImg){

        editImg.setImg1(uploadpath+"/"+editImg.getImg1());
        editImg.setImg2(uploadpath+"/"+editImg.getImg2());
        return Result.OK(openApiService.editImg(editImg));
    }

    @Autowired
    private IAiOrderService orderService;
    @GetMapping("/createOrder")
    public Result createOrder(Integer type,String paymentId){

        return Result.OK(orderService.createScoreOrder(type,paymentId));
    }


    @PostMapping("/webhook")
    public Result webhook(@RequestBody JSONObject rq){

        JSONObject data = WeChatPayment.wxpayNotify(rq);
        log.info("微信解密数据:{}",data);
        if(data!=null){
            orderService.paySuccess(data.getStr("out_trade_no"));
        }

        return Result.OK();
    }


}
